/*
 * Project: push-center-service-impl
 * 
 * File Created at 2014-10-11
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package com.greenline.push.service.core.tmpl;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.greenline.push.core.enums.Channels;
import com.greenline.push.core.pushcenter.AppChannelManager;
import com.greenline.push.dal.pushcenter.dataobject.AppChannelDO;
import com.greenline.push.service.core.utils.Constants;

/**
 * appid对应的channel缓存
 * 
 * @author weirui.shenwr
 * @date 2014-10-11
 * @version V1.0
 */
public class AppChannelCache {
    @Resource
    private AppChannelManager appChannelManager;

    private Map<Long, Channels> data = new ConcurrentHashMap<Long, Channels>();
    private Map<Long, String> appMap = new ConcurrentHashMap<Long, String>();
    private Logger log = Constants.LOG_CACHE;

    @SuppressWarnings("unused")
    private void initial() {
        log.info("AppChannelCache initial start=====");
        List<AppChannelDO> appChannelList = appChannelManager.listByExample(null);
        for (AppChannelDO appChannel : appChannelList) {
            if (StringUtils.isNotBlank(appChannel.getExtAppId()) && StringUtils.isNotBlank(appChannel.getAppChannel())) {
                data.put(appChannel.getId(), Channels.getChannelbyType(appChannel.getAppChannel()));
                appMap.put(appChannel.getId(), appChannel.getExtAppId());
            }
        }
        log.info("AppChannelCache initial end=====");
    }

    public void reload() {
        log.info(" appchannel cache reload start=======");
        List<AppChannelDO> appChannelList = appChannelManager.listByExample(null);
        if (null != appChannelList) {
            // clear old keys
            data.clear();
            appMap.clear();
            // insert
            for (AppChannelDO appChannel : appChannelList) {
                if (StringUtils.isNotBlank(appChannel.getExtAppId()) && StringUtils.isNotBlank(appChannel.getAppChannel())) {
                    data.put(appChannel.getId(), Channels.getChannelbyType(appChannel.getAppChannel()));
                    appMap.put(appChannel.getId(), appChannel.getExtAppId());
                }

            }
        }
        log.info("appchannel cache reload  end=======");
    }

    public Channels getChannel(Long appChannelId) {
        return data.get(appChannelId);
    }

    public String getExtAppId(Long appChannelId) {
        return appMap.get(appChannelId);
    }
}
